package com.splant.android.smart.UILevel.Community.fragments;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.splant.android.R;
import com.splant.android.smart.ModelLevel.DetectorListBean;

import com.splant.android.smart.SystemDefinedLevel.LRecyclerview.base.BaseMultiAdapter;
import com.splant.android.smart.SystemDefinedLevel.LRecyclerview.base.SuperViewHolder;

/**
 * Created by Administrator on 2017/7/26.
 */

public class Garden_DetectorList_Adapter extends BaseMultiAdapter<DetectorListBean.Detector> {

    public Garden_DetectorList_Adapter(Context context) {
        super(context);
        mContext=context;
        addItemType(0,R.layout.garden_detectorlist_item);
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int position) {

        View v_abnState=holder.getView(R.id.v_abnState);
        ImageView img_plant=holder.getView(R.id.img_plant);
        TextView tv_detectorName=holder.getView(R.id.tv_detectorName);
        TextView tv_waterName=holder.getView(R.id.tv_waterName);
        TextView tv_plantName=holder.getView(R.id.tv_plantName);
        ImageView img_electricity=holder.getView(R.id.img_electricity);
        TextView tv_offLine=holder.getView(R.id.tv_offline);
        TextView tv_switchNo=holder.getView(R.id.tv_switchNo);
        RelativeLayout rl_detector=holder.getView(R.id.rl_detector);

        DetectorListBean.Detector detector=getDataList().get(position);

        if (detector.getAbnState()==0)
           v_abnState.setBackgroundResource(R.color.garden_plant_normal);
        else
            v_abnState.setBackgroundResource(R.color.garden_plant_abnormal);

        Glide.with(mContext)
                .load(detector.getPlantImgUrl())
                .dontAnimate()
                .placeholder(R.mipmap.garden_ic_plantimg_fail)
                .into(img_plant);

        tv_detectorName.setText(detector.getDetectorName());
        tv_waterName.setText(detector.getWaterName());
        tv_plantName.setText(detector.getPlantName());

        if (detector.getDetectorState()==1)
        {
             tv_offLine.setVisibility(View.GONE);
             img_electricity.setVisibility(View.VISIBLE);
        }
        else
        {
            tv_offLine.setVisibility(View.VISIBLE);
            img_electricity.setVisibility(View.GONE);

            if (detector.getDetectorState()==2)
            {
                tv_offLine.setText("离线");
            }
            else if (detector.getDetectorState()==3)
            {
                tv_offLine.setText("未组网");
            }
        }

        if (detector.getAutoWater()!=null)
        {
            tv_switchNo.setText("开关"+detector.getAutoWater().getSwitchNo());
        }
        rl_detector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null)
                    mListener.onItemChick(position);
            }
        });
    }

    private onItemClickListener mListener = null;

    public void setOnItemClickListener(onItemClickListener mListener)
    {
        this.mListener=mListener;
    }

    public interface onItemClickListener {
        void onItemChick(int pos);
    }

}
